home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / ImageView.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  9.6 KB  |  328 lines

  1. package horst;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Rectangle;
  10. import java.awt.Shape;
  11. import java.awt.Toolkit;
  12. import java.awt.image.ImageObserver;
  13. import java.net.URL;
  14. import javax.swing.SwingUtilities;
  15.  
  16. public class ImageView extends View implements ImageObserver, Runnable {
  17.    Image m_image;
  18.    int m_horzInsets;
  19.    int m_vertInsets;
  20.    int m_width;
  21.    int m_height;
  22.    int m_altWidth;
  23.    int m_borderSize;
  24.    String m_alt;
  25.    boolean m_bHavePixels = false;
  26.    Color m_borderColor;
  27.    static Font m_altFont;
  28.    static int ICON_DRAWING_SIZE = 15;
  29.  
  30.    public ImageView(View parent, Element e, HTMLPane container) {
  31.       super(parent, e, container);
  32.    }
  33.  
  34.    void drawMissingImageIcon(Graphics g, int x, int y, int w, int h) {
  35.       int missingIconWidth = 15;
  36.       Color brightColor = Color.decode("#dcdcdc");
  37.       Color darkColor = brightColor.darker().darker();
  38.       int xOffset = (w - missingIconWidth) / 2;
  39.       int yOffset = (h - missingIconWidth) / 2;
  40.       g.setColor(brightColor);
  41.       g.drawLine(x + xOffset, y + yOffset, x + xOffset + missingIconWidth, y + yOffset);
  42.       g.drawLine(x + xOffset, y + yOffset, x + xOffset, y + yOffset + missingIconWidth);
  43.       g.setColor(darkColor);
  44.       g.drawLine(x + xOffset, y + yOffset + missingIconWidth, x + xOffset + missingIconWidth, y + yOffset + missingIconWidth);
  45.       g.drawLine(x + xOffset + missingIconWidth, y + yOffset, x + xOffset + missingIconWidth, y + yOffset + missingIconWidth);
  46.       g.setColor(Color.red);
  47.       int iconOffset = (missingIconWidth - 5) / 2;
  48.       int xStart = x + xOffset + iconOffset;
  49.       int yStart = y + yOffset + iconOffset;
  50.       g.drawLine(xStart, yStart, xStart + 1, yStart);
  51.       g.drawLine(xStart + 4, yStart, xStart + 5, yStart);
  52.       g.drawLine(xStart + 1, yStart + 1, xStart + 4, yStart + 1);
  53.       g.drawLine(xStart + 2, yStart + 2, xStart + 3, yStart + 2);
  54.       g.drawLine(xStart + 1, yStart + 3, xStart + 4, yStart + 3);
  55.       g.drawLine(xStart, yStart + 4, xStart + 1, yStart + 4);
  56.       g.drawLine(xStart + 4, yStart + 4, xStart + 5, yStart + 4);
  57.    }
  58.  
  59.    protected void flushResources() {
  60.       if (this.m_image != null) {
  61.          this.m_image.flush();
  62.          this.m_image = null;
  63.       }
  64.  
  65.    }
  66.  
  67.    protected int getMinimumSpan(int axis) {
  68.       return this.getPreferredSpan(axis);
  69.    }
  70.  
  71.    protected int getPreferredSpan(int axis) {
  72.       switch (axis) {
  73.          case 0:
  74.             if (super.m_prefHeight != -1) {
  75.                return super.m_prefHeight;
  76.             } else {
  77.                if (this.m_height > 0) {
  78.                   super.m_prefHeight = this.m_height + this.m_vertInsets;
  79.                } else {
  80.                   if (this.m_image != null) {
  81.                      return Math.max(this.m_image.getHeight((ImageObserver)null), 0) + this.m_vertInsets;
  82.                   }
  83.  
  84.                   super.m_prefHeight = ICON_DRAWING_SIZE + this.m_vertInsets;
  85.                }
  86.  
  87.                return super.m_prefHeight;
  88.             }
  89.          case 1:
  90.             if (super.m_prefWidth != -1) {
  91.                return super.m_prefWidth;
  92.             } else {
  93.                if (this.m_width > 0) {
  94.                   super.m_prefWidth = this.m_width + this.m_horzInsets;
  95.                } else {
  96.                   if (this.m_image != null) {
  97.                      return Math.max(this.m_image.getWidth((ImageObserver)null), 0) + this.m_vertInsets;
  98.                   }
  99.  
  100.                   super.m_prefWidth = ICON_DRAWING_SIZE + this.m_altWidth + this.m_horzInsets;
  101.                }
  102.  
  103.                return super.m_prefWidth;
  104.             }
  105.          default:
  106.             return 30;
  107.       }
  108.    }
  109.  
  110.    protected String getToolTipText() {
  111.       return this.m_alt != null ? this.m_alt : "";
  112.    }
  113.  
  114.    public boolean imageUpdate(Image img, int flags, int x, int y, int width, int height) {
  115.       if (this.m_image == null) {
  116.          return false;
  117.       } else if ((flags & 192) != 0) {
  118.          this.m_image = null;
  119.          return false;
  120.       } else {
  121.          boolean resized = false;
  122.          if ((flags & 2) != 0 && this.m_height == -1) {
  123.             this.m_height = height;
  124.             resized = true;
  125.          }
  126.  
  127.          if ((flags & 1) != 0 && this.m_width == -1) {
  128.             this.m_width = width;
  129.             resized = true;
  130.          }
  131.  
  132.          if (resized) {
  133.             SwingUtilities.invokeLater(this);
  134.             return true;
  135.          } else {
  136.             if ((flags & 56) != 0) {
  137.                this.m_bHavePixels = true;
  138.                super.m_container.repaint(super.m_bounds);
  139.             }
  140.  
  141.             return true;
  142.          }
  143.       }
  144.    }
  145.  
  146.    protected void init() {
  147.       super.m_alignment = Utilities.setAlignmentProperty(false, -1, "align", super.m_elem.getAttributes());
  148.       String content = (String)super.m_elem.getAttribute("imagecontent");
  149.       if (content != null && content.equals("true")) {
  150.          ((View)this).setInsets(5, 5, 5, 5);
  151.       } else if (this.isFloater()) {
  152.          ((View)this).setInsets(3, 3, 3, 3);
  153.       } else {
  154.          ((View)this).setInsets(0, 0, 0, 0);
  155.          super.m_insets.left = super.m_insets.right = Utilities.setIntegerProperty(0, "hspace", super.m_elem.getAttributes());
  156.          super.m_insets.top = super.m_insets.bottom = Utilities.setIntegerProperty(0, "vspace", super.m_elem.getAttributes());
  157.       }
  158.  
  159.       this.m_borderColor = Utilities.setColorProperty(Color.black, "color", super.m_elem.getAttributes());
  160.       this.m_alt = (String)super.m_elem.getAttribute("alt");
  161.       this.m_altWidth = 0;
  162.       if (m_altFont == null) {
  163.          m_altFont = new Font("Dialog", 2, 10);
  164.       }
  165.  
  166.       if (this.m_alt != null) {
  167.          FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(m_altFont);
  168.          this.m_altWidth = fm.stringWidth(this.m_alt);
  169.       }
  170.  
  171.       this.m_width = Utilities.setIntegerProperty(-1, "width", super.m_elem.getAttributes());
  172.       this.m_height = Utilities.setIntegerProperty(-1, "height", super.m_elem.getAttributes());
  173.       this.m_borderSize = Utilities.setIntegerProperty(-1, "border", super.m_elem.getAttributes());
  174.       if (this.m_borderSize == -1) {
  175.          this.m_borderSize = this.isLink() ? 2 : 0;
  176.       }
  177.  
  178.       this.m_horzInsets = super.m_insets.left + super.m_insets.right + 2 * this.m_borderSize;
  179.       this.m_vertInsets = super.m_insets.top + super.m_insets.bottom + 2 * this.m_borderSize;
  180.    }
  181.  
  182.    protected boolean isContainerView() {
  183.       return false;
  184.    }
  185.  
  186.    protected boolean isFloater() {
  187.       return super.m_alignment == 0 || super.m_alignment == 2;
  188.    }
  189.  
  190.    protected boolean isLink() {
  191.       boolean bLink = super.isLink();
  192.       if (!bLink) {
  193.          bLink = super.m_elem.isAttributeDefined("ismap") || super.m_elem.isAttributeDefined("usemap");
  194.       }
  195.  
  196.       return bLink;
  197.    }
  198.  
  199.    protected Rectangle layout(int x, int y, int width, LayoutInfo info) {
  200.       super.m_bounds = new Rectangle(x, y, this.getPreferredSpan(1), this.getPreferredSpan(0));
  201.       return super.m_bounds;
  202.    }
  203.  
  204.    protected void loadResources() {
  205.       if (super.m_container.m_preferences.m_bLoadImages) {
  206.          URL u = null;
  207.          String src = (String)super.m_elem.getAttribute("src");
  208.          if (src != null && src.length() > 0) {
  209.             u = Utilities.getURL(super.m_elem.getDocument().getBaseURL(), src);
  210.          }
  211.  
  212.          if (u != null) {
  213.             this.m_image = Toolkit.getDefaultToolkit().getImage(u);
  214.             if (this.m_image != null) {
  215.                if (super.m_container.m_preferences.m_bProgressiveDisplay) {
  216.                   if (this.m_height > 0 && this.m_width > 0) {
  217.                      this.m_bHavePixels = Toolkit.getDefaultToolkit().prepareImage(this.m_image, this.m_width, this.m_height, this);
  218.                   } else {
  219.                      this.m_bHavePixels = Toolkit.getDefaultToolkit().prepareImage(this.m_image, -1, -1, this);
  220.                      if (this.m_bHavePixels) {
  221.                         super.m_prefWidth = Math.max(this.m_image.getWidth((ImageObserver)null), 0) + this.m_horzInsets;
  222.                         super.m_prefHeight = Math.max(this.m_image.getHeight((ImageObserver)null), 0) + this.m_vertInsets;
  223.                      }
  224.                   }
  225.                } else {
  226.                   MediaTracker tracker = new MediaTracker(super.m_container);
  227.  
  228.                   try {
  229.                      if (this.m_height > 0 && this.m_width > 0) {
  230.                         tracker.addImage(this.m_image, 0, this.m_width, this.m_height);
  231.                      } else {
  232.                         tracker.addImage(this.m_image, 0);
  233.                      }
  234.  
  235.                      tracker.waitForID(0);
  236.                      this.m_bHavePixels = !tracker.isErrorAny();
  237.                      if (!this.m_bHavePixels) {
  238.                         this.m_image = null;
  239.                      }
  240.                   } catch (InterruptedException var4) {
  241.                      this.m_image = null;
  242.                   }
  243.                }
  244.             }
  245.          }
  246.  
  247.       }
  248.    }
  249.  
  250.    public void paint(Graphics g, Shape alloc) {
  251.       if (super.m_bounds.intersects(alloc.getBounds())) {
  252.          int x = super.m_bounds.x + super.m_insets.left + this.m_borderSize;
  253.          int y = super.m_bounds.y + super.m_insets.top + this.m_borderSize;
  254.          int w = super.m_bounds.width - super.m_insets.left - super.m_insets.right - 2 * this.m_borderSize;
  255.          int h = super.m_bounds.height - super.m_insets.top - super.m_insets.bottom - 2 * this.m_borderSize;
  256.          if (this.m_image != null && this.m_bHavePixels) {
  257.             if (super.m_elem.isAttributeDefined("imagecontent")) {
  258.                g.drawImage(this.m_image, x, y, this);
  259.             } else {
  260.                g.drawImage(this.m_image, x, y, w, h, this);
  261.             }
  262.  
  263.             if (this.m_borderSize > 0) {
  264.                Color c = this.isLink() ? super.m_elem.getDocument().getLinkColor() : super.m_elem.getDocument().getTextColor();
  265.                g.setColor(c);
  266.  
  267.                for(int i = 1; i <= this.m_borderSize; ++i) {
  268.                   g.drawRect(x - i, y - i, w - 1 + i + i, h - 1 + i + i);
  269.                }
  270.             }
  271.          } else {
  272.             int borderSize = 1;
  273.             if (this.m_image != null) {
  274.                g.setColor(Color.black);
  275.                g.drawRect(super.m_bounds.x, super.m_bounds.y, super.m_bounds.width - 1, super.m_bounds.height - 1);
  276.             } else {
  277.                Utilities.drawBorder(g, super.m_bounds);
  278.             }
  279.  
  280.             int xOffset = 0;
  281.             int padding = 5;
  282.             int twoPadding = 2 * padding;
  283.             if (this.m_image == null && super.m_bounds.width > ICON_DRAWING_SIZE + twoPadding && super.m_bounds.height > ICON_DRAWING_SIZE + twoPadding) {
  284.                this.drawMissingImageIcon(g, x + padding, y + padding, ICON_DRAWING_SIZE, ICON_DRAWING_SIZE);
  285.                xOffset = twoPadding + ICON_DRAWING_SIZE;
  286.             }
  287.  
  288.             String text = "";
  289.             if (this.m_image == null && this.m_alt != null) {
  290.                text = this.m_alt;
  291.             } else if (this.m_image != null) {
  292.                text = "Loading...";
  293.             }
  294.  
  295.             FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(m_altFont);
  296.             int len = fm.stringWidth(text);
  297.             int xSpace = super.m_bounds.width - 2 * borderSize - super.m_insets.left - super.m_insets.right - xOffset - len;
  298.             int ySpace = super.m_bounds.height - 2 * borderSize - super.m_insets.top - super.m_insets.bottom - fm.getHeight();
  299.             if (xSpace > 0 && ySpace > 0 && text.length() > 0) {
  300.                Font oldFont = g.getFont();
  301.                g.setFont(m_altFont);
  302.                int fheight = fm.getHeight();
  303.                Color oldColor = g.getColor();
  304.                g.setColor(super.m_container.m_document.getTextColor());
  305.                g.drawString(text, super.m_bounds.x + borderSize + super.m_insets.left + xOffset, super.m_bounds.y + borderSize + super.m_insets.top + ySpace / 2 + fm.getHeight() / 2);
  306.                g.setColor(oldColor);
  307.                g.setFont(oldFont);
  308.             }
  309.          }
  310.  
  311.          ((View)this).drawDebugBox(g, Color.blue);
  312.       }
  313.    }
  314.  
  315.    protected void paintFocusBox(Graphics g, Shape alloc) {
  316.       if (super.m_elem.getDrawFocusBox() && super.m_bounds.intersects(alloc.getBounds())) {
  317.          Utilities.drawFocusBorder(g, super.m_bounds);
  318.       }
  319.  
  320.    }
  321.  
  322.    public void run() {
  323.       Utilities.debugOut("ImageView calling forceLayout");
  324.       ((View)this).invalidate();
  325.       ((View)this).forceLayout();
  326.    }
  327. }
  328.